<template>
  <div class="component-score-order-growth-growth-config distribution-recruit-set gray-box top-redius border-bottom-gray">
    <div class="title">
      {{ $t('user.growthSetting') }}
    </div>
    <!-- native modifier has been removed, please confirm whether the function has been affected  -->
    <el-form
      ref="dataFormRef"
      label-width="auto"
      class="set-form"
      label-position="left"
      :rules="dataRule"
      :model="dataForm"
      @submit.prevent
    >
      <el-form-item
        :label="$t('user.growthSwitch')"
        style="width:440px;margin-bottom: 30px"
        prop="shopGrowthSwitch"
      >
        <el-switch v-model="dataForm.shopGrowthSwitch" />
      </el-form-item>
      <el-form-item
        v-if="dataForm.shopGrowthSwitch"
        :label="$t('user.perPurchase')"
        style="width:440px;margin-bottom: 30px"
        prop="buyPrice"
      >
        <!-- native modifier has been removed, please confirm whether the function has been affected  -->
        <el-input
          v-model="dataForm.buyPrice"
          type="number"
          @keydown="onHandleKeydown"
          @change="onSetShopGetScore"
        >
          <template #append>
            {{ $t('coupon.yuan') }}{{ $t('user.getOneGrowth') }}
          </template>
        </el-input>
        <!-- <span class="tips">元可获取1积分</span> -->
      </el-form-item>
      <el-form-item
        v-if="dataForm.shopGrowthSwitch"
        :label="$t('user.completedOrderGet')"
        style="width:440px"
        prop="buyOrder"
      >
        <!-- native modifier has been removed, please confirm whether the function has been affected  -->
        <el-input
          v-model="dataForm.buyOrder"
          type="number"
          @keydown="onHandleKeydown"
          @change="onSetShopComplete"
        >
          <template #append>
            {{ $t('user.growth') }}
          </template>
        </el-input>
        <!-- <span class="tips">元可获取1积分</span> -->
      </el-form-item>
    </el-form>
    <div
      class="default-btn"
      @click="onSubmit()"
    >
      {{ $t('sysManagement.save') }}
    </div>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'

const dataForm = ref({
  id: 0,
  buyPrice: 0.0,
  shopGrowthSwitch: false,
  buyOrder: 0.0
})
const dataRule = {
  buyPrice: [
    { required: true, message: $t('user.consumptionGetGrowthCannotEmpty'), trigger: 'blur' }
  ],
  buyOrder: [
    { required: true, message: $t('user.orderAcquisitionCannotEmpty'), trigger: 'blur' }
  ]
}
onMounted(() => {
  getData()
})

// 获取数据
const getData = () => {
  http({
    url: http.adornUrl('/user/GrowthConfig/info/' + 'GROWTH_CONFIG'),
    method: 'get',
    params: http.adornParams()
  }).then(({ data }) => {
    nextTick(() => {
      if (data.shopGrowthSwitch) {
        dataForm.value = data
      }
    })
  })
}

const onHandleKeydown = (e) => {
  const key = e.key
  // 不允许输入'e'和'.'
  if (key === 'e' || key === '.') {
    e.returnValue = false
    return false
  }
  return true
}
const onSetShopGetScore = () => {
  if (dataForm.value.buyPrice === '' || isNaN(dataForm.value.buyPrice)) {
    ElMessage.error($t('formData.pleaseThan0'))
    return
  }
  const num = Math.round(dataForm.value.buyPrice)
  dataForm.value.buyPrice = num < 1 ? 1 : num
  if (num >= 100000000) {
    dataForm.value.buyPrice = 100000000
  }
}

const onSetShopComplete = () => {
  if (dataForm.value.buyOrder === '' || isNaN(dataForm.value.buyOrder)) {
    ElMessage.error($t('formData.pleaseThan0'))
    return
  }
  const num = Math.round(dataForm.value.buyOrder)
  dataForm.value.buyOrder = num < 1 ? 1 : num
  if (num >= 100000000) {
    dataForm.value.buyOrder = 100000000
  }
}
// 提交表单
const dataFormRef = ref(null)
const onSubmit = () => {
  dataFormRef.value?.validate((valid) => {
    if (valid) {
      if (dataForm.value.shopGrowthSwitch && !dataForm.value.buyPrice) {
        ElMessage.error($t('user.pleaseEnterCorrectValue'))
        dataForm.value.buyPrice = ''
        return
      } else if (dataForm.value.shopGrowthSwitch && !dataForm.value.buyOrder) {
        ElMessage.error($t('user.pleaseEnterCorrectValue'))
        dataForm.value.buyOrder = ''
        return
      }
      const param = dataForm.value
      http({
        url: http.adornUrl('/user/GrowthConfig'),
        method: 'post',
        data: http.adornData(param)
      }).then(() => {
        ElMessage({
          message: $t('publics.operation'),
          type: 'success',
          duration: 1500
        })
      }).catch(() => {

      })
    }
  })
}

</script>
<style lang="scss" scoped>
.component-score-order-growth-growth-config{
  :deep(.distribution-withdraw-cash-set) {
    .input-tips {
      font-size: 12px;
    }
  }
  // 全局css 加上以下代码，可以隐藏上下箭头

  // 取消input的上下箭头
  :deep(input::-webkit-inner-spin-button) {
    -webkit-appearance: none !important;

  }
  :deep(input::-webkit-outer-spin-button){
    -webkit-appearance: none !important;

  }
}
</style>
